package com.weng.platform.apiManager.doc.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.apiManager.doc.entity.DocTemplate;
import com.weng.platform.apiManager.doc.model.vo.DocTemplateVo;
import com.weng.platform.apiManager.doc.service.IDocTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * api文档模板 API实现
 *
 * @author wengzhonghui
 * @date 2020-09-25 18:40:15
 *
 */  
@Slf4j
@RestController
@RequestMapping("/doc-template")
@Api(value = "api文档模板api", tags = {"api文档模板操作接口"})
public class DocTemplateApi extends BaseController {
    @Autowired
    private IDocTemplateService docTemplateService;

    /**
     * 功能描述:单个获取api文档模板
     */
    @ApiOperation(value = "获取api文档模板", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        DocTemplate docTemplate = docTemplateService.getById(id);
        return success(docTemplate);
    }


    /**
     * 条件 分页查询api文档模板
     */
    @ApiOperation(value = "分页获取api文档模板", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<DocTemplate> iPage = docTemplateService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增api文档模板
	 */
	@ApiOperation(value = "新增api文档模板", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody DocTemplateVo docTemplateReq){
		DocTemplate docTemplate = Convert.convert(DocTemplate.class, docTemplateReq);
		docTemplateService.add(docTemplate);
		return success();
	}

	/**
	 * 更新api文档模板
	 */
	@ApiOperation(value = "更新api文档模板", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody DocTemplateVo docTemplateReq){
		DocTemplate docTemplate = Convert.convert(DocTemplate.class, docTemplateReq);
		docTemplateService.update(docTemplate);
		return success();
	}


	/**
	 * 删除api文档模板
	 */
	@ApiOperation(value = "ids 删除api文档模板", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = docTemplateService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}
}
